// pages/music-player/music-player.js
import {
  getSongDetail,
  getSongLyric
} from "../../../services/player"
import { parseLyric } from "../../../utils/parse-lyric"

import { throttle } from "underscore"

import playerStore, { audioContext } from "../../../store/playerStore"

const app = getApp()


const modeNames = ["order", "repeat", "random"]

Page({
  data: {
    id: 0,

    stateKeys: ["id", "currentSong", "durationTime", "currentTime", "lyricInfos", "currentLyricText", "currentLyricIndex", "isPlaying", "playModeIndex"],

    currentSong: {},
    lyricInfos: [],
    currentLyricText: "",
    currentLyricIndex: 0,

    isPlaying: false,

    currentTime: 0,
    durationTime: 0,

    playSongIndex: 0,
    playSongList: [],
    isFirstPlay: true,

    playModeIndex: 0, // 0：顺序播放 1：单曲循环 2：随机播放
    playModeName: "order",

    pageTitles: ["歌曲", "歌词"],
    currentPage: 0,
    contentHeight: 0,
    sliderValue: 0,
    isSliderhanging: false,
    lyricScrollTop: 0,
  },

  onLoad(options) {
    //获取设备信息
    this.setData({
      statusHeight: app.globalData.statusHeight,
      contentHeight: app.globalData.contentHeight
    })

    //获取传入的id
    const id = options.id

    //根据id播放歌曲
    if (id) { playerStore.dispatch("playMusicWithSongIdAction", id) }


    //获取store共享数据
    playerStore.onStates(["playSongList", "playSongIndex"], this.getPlaySongInfosHandler)
    playerStore.onStates(this.data.stateKeys, this.getPlayerInfosHandler)
  },

  updateProgress: throttle(function (currentTime) {
    if (this.data.isSliderChanging) return
    //记录当前时间
    //修改sliderValue
    const sliderValue = currentTime / this.data.durationTime * 100
    this.setData({
      currentTime,
      sliderValue
    })
  }, 800, { leading: false, trailing: false }),


  // 事件监听
  onNavBackTap() {
    wx.navigateBack()
  },
  onSwiperChange(event) {
    this.setData({
      currentPage: event.detail.current
    })
  },
  onNavTabItemTap(event) {
    const index = event.currentTarget.dataset.index
    this.setData({
      currentPage: index
    })
  },
  onSliderChange(event) {
    //获得点击的滑块的位置对应的值
    const value = event.detail.value

    //计算出要播放的位置时间
    const currentTime = value / 100 * this.data.durationTime

    //设置播放器播放计算出的时间
    audioContext.seek(currentTime / 1000)
    this.setData({ currentTime, isSliderChanging: false, sliderValue: value })
    if (!this.data.isPlaying) {
      this.setData({
        isPlaying: true
      })
    }
  },
  onSliderChanging: throttle(function (event) {
    //获得滑动到的位置的value
    const value = event.detail.value

    //计算出要播放的位置时间
    const currentTime = value / 100 * this.data.durationTime
    this.setData({ currentTime })

    //当前正在滑动
    this.data.isSliderChanging = true
  }, 100),


  onPlayOrPause() {
    playerStore.dispatch("changeMusicStatusAction")
  },
  onPrevBtnTap() {
    playerStore.dispatch("playNewMusicAction", false)
  },
  onNextBtnTap() {
    playerStore.dispatch("playNewMusicAction")
  },


  onModeBtnTap() {
    playerStore.dispatch("changePlayModeAction")
  },

  //store共享数据
  getPlaySongInfosHandler({ playSongList, playSongIndex }) {
    if (playSongList) {
      this.setData({ playSongList })
    }
    if (playSongIndex !== undefined) {
      this.setData({ playSongIndex })
    }
  },
  getPlayerInfosHandler({ id, currentSong, durationTime, currentTime, lyricInfos, currentLyricIndex, currentLyricText, isPlaying, playModeIndex, playSongIndex }) {
    if (id !== undefined) {
      this.setData({ id })
    }
    if (currentSong) {
      this.setData({ currentSong })
    }
    if (durationTime !== undefined) {
      this.setData({ durationTime })
    }
    if (currentTime !== undefined) {
      this.updateProgress(currentTime)
    }
    if (lyricInfos) {
      this.setData({ lyricInfos })
    }
    if (currentLyricText) {
      this.setData({ currentLyricText })
    }
    if (currentLyricIndex !== undefined) {
      this.setData({ currentLyricIndex, lyricScrollTop: currentLyricIndex * 35 })
    }
    if (isPlaying !== undefined) {
      this.setData({ isPlaying })
    }
    if (playModeIndex !== undefined) {
      this.setData({ playModeName: modeNames[playSongIndex] })
    }

  },

  onUnload() {
    playerStore.offStates(["playSongList", "palySongIndex"], this.getPlaySongInfosHandler)
    playerStore.offStates(this.data.stateKeys, this.getPlayerInfosHandler)
  }

})